package controller.link.battle;

import view.client.Console;
import view.client.Frame;
import model.Hearer;
import view.client.PanelBattle;
import view.client.PanelGame;
import java.io.Serializable;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JTextArea;
import model.game.Item;
import controller.Link;
import model.game.Player;
import model.game.Skill;
import model.User;
import model.Attendant;
import model.Battle;
import model.Server;

public class ChallengeAccepted implements Link, Serializable {

    private Player player;
    private String nameDesafiante;

    public ChallengeAccepted(String nameDesafiante) {
        this.nameDesafiante = nameDesafiante;
    }

    public ChallengeAccepted(Hearer h, String nameDesafiante) {
        this.nameDesafiante = nameDesafiante;
        h.send((Link) this);
    }

    @Override
    public void serverUse(Server server, Attendant at) {
        User user = at.getUser();
        //manda para o atendente que aceitou o desafio o desafiante como player
        this.player = server.getWorld().getMap().findPlayer(nameDesafiante);
        at.send((Link) this);
        //manda para o desafiante o player que aceitou o desafio
        this.player = user.getPlayer();
        server.searchAtendente(nameDesafiante).send((Link) this);
        //start battle
        Battle mg = new Battle(at, user.getPlayer(), server.searchAtendente(nameDesafiante), server.searchAtendente(nameDesafiante).getUser().getPlayer(), server);
        server.addBattle(mg);
        mg.init();
    }

    @Override
    public void clientUse(Console console) {
        PanelGame pg = console.getPg();
        PanelBattle pb = console.getPb();
        JTextArea csl = console.getTxtArea();
        Frame frame = console.getFrame();
        ArrayList<Skill> skills = console.getSkills();
        ArrayList<Item> items = console.getItems();
        skills = player.getSkills();
        items = player.getItems();
        pb = new PanelBattle(console);
        console.setPb(pb);
        console.setSkills(player.getSkills());
        console.setItems(player.getItems());
        pb.getLblName2().setText(pg.getLblName().getText());
        pb.getPbLife().setStringPainted(true);
        pb.getPbLife().setMaximum(pg.getPbVida().getMaximum());
        pb.getPbLife().setStringPainted(true);
        pb.getPbLife().setValue(pg.getPbVida().getValue());
        pb.getPbLife().setString(pg.getPbVida().getString());
        pb.getPbLife().repaint();
        pb.getPbEnergy().setStringPainted(true);
        pb.getPbEnergy().setMaximum(pg.getPbEnergia().getMaximum());
        pb.getPbEnergy().setStringPainted(true);
        pb.getPbEnergy().setValue(pg.getPbEnergia().getValue());
        pb.getPbEnergy().setString(pg.getPbEnergia().getString());
        pb.getPbEnergy().repaint();
        pb.getLblName().setText(player.getName());
        pb.getPbEnemyLife().setStringPainted(true);
        pb.getPbEnemyLife().setMaximum(player.getTotalLife());
        pb.getPbEnemyLife().setStringPainted(true);
        pb.getPbEnemyLife().setValue(player.getLife());
        pb.getPbEnemyLife().setString(Integer.toString(player.getLife()) + "/" + Integer.toString(player.getTotalLife()));
        pb.getPbEnemyLife().repaint();
        pb.getPbEnemyEnergy().setStringPainted(true);
        pb.getPbEnemyEnergy().setMaximum(player.getTotalEnergy());
        pb.getPbEnemyEnergy().setStringPainted(true);
        pb.getPbEnemyEnergy().setValue(player.getEnergy());
        pb.getPbEnemyEnergy().setString(Integer.toString(player.getEnergy()) + "/" + Integer.toString(player.getTotalEnergy()));
        pb.getPbEnemyEnergy().repaint();
        csl = pb.getTxtArea();
        pb.getListAction().setEnabled(false);

        for (int i = 0; i < skills.size(); i++) {
            pb.getModelSkill().add(i, skills.get(i).getName());
        }
        for (int i = 0; i < items.size(); i++) {
            pb.getModelConsumable().add(i, items.get(i).getName());
        }
        frame.loadBattle(pb);
    }
}
